using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Modularity;
using Volo.Abp.AspNetCore;     // 依赖 ABP ASP.NET Core 基础模块
using RealTimeChat.Services;   // AuthInterceptor, ChatServiceImpl

namespace RealTimeChat
{
    [DependsOn(typeof(AbpAspNetCoreModule))]
    public class ChatGrpcModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;

            services.AddSingleton<AuthInterceptor>();

            services.AddGrpc(options =>
            {
                options.EnableDetailedErrors = true;                     // 详细错误
                options.MaxReceiveMessageSize = 4 * 1024 * 1024;           // 接收大小限制
                options.MaxSendMessageSize = 4 * 1024 * 1024;           // 发送大小限制
                options.ResponseCompressionAlgorithm = "gzip";                   // 服务器响应使用 gzip
                options.Interceptors.Add(typeof(AuthInterceptor));                // 注入拦截器
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<ChatServiceImpl>();
            });
        }

    }
}
